package com.ailk.tcm.hffw.android.common.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ailk.tcm.R;
import com.ailk.tcm.hffw.android.utils.UnitUtil;

public abstract class TabMenuActivity extends FragmentActivity{
	
	private static final int MENU_TITLE_COLOR = Color.rgb(0, 0, 0);
	private static final int MENU_TITLE_FOCUS_COLOR = Color.WHITE;
	
	protected FrameLayout contentContainer;
	protected ViewGroup menuContainer;
	
	protected FragmentManager fragManager;
	
	private int selectedMenu = -1;
	private ColorStateList f;
	
	private List<TextView> msgAlertViews = new ArrayList<TextView>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		fragManager = getFragmentManager();
		setContentView(R.layout.activity_tabmenu);
		contentContainer = (FrameLayout) findViewById(R.id.content_container);
		menuContainer = (ViewGroup) findViewById(R.id.menu_container);
		createMenu();
		super.onCreate(savedInstanceState);
	}
	
	/**
	 * 由子类实现，返回要添加的菜单未聚焦时的图标
	 * @return
	 */
	protected abstract int[] getMenuImageResources();
	/**
	 * 由子类实现，返回要添加的菜单聚焦图标
	 * @return
	 */
	protected abstract int[] getMenuFocusImageResources();
	/**
	 * 由子类实现，返回要添加的菜单标题
	 * @return
	 */
	protected abstract String[] getMenuTitles();
	/**
	 * 由子类实现，返回要添加的菜单对应fragment
	 * @return
	 */
	protected abstract Class<? extends Fragment>[] getMenuFragmentClasses();
	
	/**
	 * 子类可选择覆盖实现，菜单切换时触发，默认效果是切换getMenuFragmentClasses()方法返回的fragment
	 * @param oldMenuIndex		为-1时表示切换之前未选中过菜单项
	 * @param newMenuIndex
	 * @return		返回false时中断此次切换
	 */
	protected boolean onMenuSelected(int oldMenuIndex, int newMenuIndex){
		Class<? extends Fragment>[] menuFragmentClasses = getMenuFragmentClasses();
		//切换fragment
		if(menuFragments[newMenuIndex] == null){
			try {
				menuFragments[newMenuIndex] = menuFragmentClasses[newMenuIndex].newInstance();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		fragManager.beginTransaction().replace(R.id.content_container, menuFragments[newMenuIndex]).commit();
		return true;
	}
	
	protected void selectMenu(int index){
		if(index == selectedMenu){
			return;
		}
		if(!onMenuSelected(selectedMenu, index)){
			return;
		}
		//清除选中状态
		if(selectedMenu >= 0){
			menuImageViews[selectedMenu].setImageResource(getMenuImageResources()[selectedMenu]);
			menuTextViews[selectedMenu].setTextColor(MENU_TITLE_COLOR);
		}
		int[] menuFocusImages = getMenuFocusImageResources();
		//设置选中状态
		menuImageViews[index].setImageResource(menuFocusImages[index]);
		menuTextViews[index].setTextColor(MENU_TITLE_FOCUS_COLOR);
		selectedMenu = index;
	}
	
	public int getSelectedMenu() {
		return selectedMenu;
	}

	private ImageView[] menuImageViews;
	private TextView[] menuTextViews;
	private Fragment[] menuFragments;
	/**
	 * 创建添加菜单
	 */
	private void createMenu(){
		int[] menuImageResources = getMenuImageResources();
		String[] menuTitles = getMenuTitles();
		menuImageViews = new ImageView[menuImageResources.length];
		menuTextViews = new TextView[menuTitles.length];
		menuFragments = new Fragment[menuTitles.length];
		int padding = UnitUtil.dip2px(2);
		for(int i = 0; i < menuTitles.length; i++){
			//创建菜单项容器
			LinearLayout menuBody = new LinearLayout(this);
			menuBody.setOrientation(LinearLayout.VERTICAL);
			menuBody.setGravity(Gravity.CENTER);
			LinearLayout.LayoutParams menuLp = 
					new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
			menuLp.weight = 1;
			menuBody.setLayoutParams(menuLp);
			menuBody.setPadding(padding, padding, padding, padding);
			//创建菜单项图片
			FrameLayout imageContainer = new FrameLayout(this);
			menuImageViews[i] = new ImageView(this);
			menuImageViews[i].setScaleType(ScaleType.FIT_CENTER);
			menuImageViews[i].setImageResource(menuImageResources[i]);
			imageContainer.addView(menuImageViews[i], FrameLayout.LayoutParams.MATCH_PARENT, 
					FrameLayout.LayoutParams.MATCH_PARENT);
			TextView msgAlertView = new TextView(this);
			msgAlertView.setBackgroundResource(R.drawable.msg_alert_background);
			msgAlertView.setTextColor(Color.WHITE);
			msgAlertView.setGravity(Gravity.CENTER);
			msgAlertView.setVisibility(View.GONE);
			FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(0, 0, Gravity.TOP|Gravity.RIGHT);
			imageContainer.addView(msgAlertView, lp);
			msgAlertViews.add(msgAlertView);
			LinearLayout.LayoutParams imageContainerLp = 
					new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0);
			imageContainerLp.weight = 1;
			menuBody.addView(imageContainer, imageContainerLp);
			//创建菜单项文字
			menuTextViews[i] = new TextView(this);
			menuTextViews[i].setTextColor(MENU_TITLE_COLOR);
			menuTextViews[i].setText(menuTitles[i]);
			LinearLayout.LayoutParams textLp = 
					new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			menuBody.addView(menuTextViews[i], textLp);
			
			menuBody.setId(i + 1);	//id必须是正数
			menuBody.setOnClickListener(onMenuClickListener);
			menuContainer.addView(menuBody);
		}
	}
	
	private OnClickListener onMenuClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			selectMenu(v.getId() - 1);	//index = id - 1,见menu.setId
		}
	};
	
	public void setMsgAlert(int index, CharSequence msg){
		TextView msgAlertView = msgAlertViews.get(index);
		int size = (int) (menuContainer.getMeasuredHeight() * 0.3);
		if(size == 0){
			size = UnitUtil.dip2px(20);
		}
		msgAlertView.getLayoutParams().width = size;
		msgAlertView.getLayoutParams().height = size;
		msgAlertView.setText(msg);
	}
	
	public void setMsgAlertVisiable(int index, boolean visiable){
		msgAlertViews.get(index).setVisibility(visiable ? View.VISIBLE : View.GONE);
	}
}
